﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace SourceBackup
{
    public partial class EditFilter : Form
    {
        public EditFilter()
        {
            InitializeComponent();
        }

        private BackupFilter selectedFilter = new BackupFilter("New filter", "*.*");
        public BackupFilter SelectedFilter
        {
            get { return selectedFilter; }
            set { selectedFilter = value; }
        }

        #region Form events
        private void EditFilter_Load(object sender, EventArgs e)
        {
            txtName.Text = selectedFilter.Name;
            lstFilters.Items.AddRange(
                (from s in selectedFilter.FilterList
                 select s).ToArray());
        } 
        #endregion

        #region Button events
        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            if (txtFileType.Text.Length > 0)
            {
                if (lstFilters.SelectedItem != null)
                {
                    lstFilters.Items[lstFilters.SelectedIndex] = txtFileType.Text;
                }
                else
                {
                    if ((from object entry in lstFilters.Items
                         where ((string)entry).ToUpper() == txtFileType.Text.ToUpper()
                         select entry).Count() > 0)
                    {
                        MessageBox.Show("An entry with this value already exist!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    lstFilters.Items.Add(txtFileType.Text);
                    lstFilters.SelectedItem = txtFileType.Text;
                }
                cmdNew_Click(null, null);
            }
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (lstFilters.SelectedItem != null)
            {
                if (MessageBox.Show("Are you sure you want to remove this file type?", "Remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    lstFilters.Items.Remove(lstFilters.SelectedItem);
                    txtFileType.Text = "";
                    cmdDelete.Enabled = false;
                }
            }
        }

        private void cmdNew_Click(object sender, EventArgs e)
        {
            lstFilters.SelectedIndex = -1;
            cmdDelete.Enabled = false;
            txtFileType.Text = "";
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length == 0)
            {
                MessageBox.Show("You must specify a name for the filter!", "Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (lstFilters.Items.Count == 0)
            {
                MessageBox.Show("The backup filter must contain at least one file filter mask!", "Filter", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                selectedFilter.Name = txtName.Text;
                selectedFilter.FilterList = new List<string>();
                selectedFilter.FilterList.AddRange(
                    (from string s in lstFilters.Items
                     select s).ToArray());
                DialogResult = DialogResult.OK;
                Close();
            }
        } 
        #endregion

        #region Listbox events
        private void lstFilters_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstFilters.SelectedItem != null)
            {
                string filter = (string)lstFilters.SelectedItem;
                txtFileType.Text = filter;
                cmdDelete.Enabled = true;
            }
        } 
        #endregion

        #region Textbox events
        private void txtFileType_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                cmdUpdate_Click(sender, e);
                e.Handled = true;
            }
            else if (e.KeyChar == char.ConvertFromUtf32(27).ToCharArray()[0])
            {
                cmdNew_Click(null, null);
                e.Handled = true;
            }
        }
        #endregion       
    }
}
